package com.flashcards;

import android.util.Log;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.ViewFlipper;
import com.flashcards.vocabulary.Vocabulary;

public class Navigation {
    private ViewFlipper flipper;
    private Vocabulary vocabulary;

    public Navigation(ViewFlipper flipper, Vocabulary vocabulary) {
        this.flipper = flipper;
        this.vocabulary = vocabulary;
    }

    public boolean onKey(int keyCode) {
        Word word = null;
        TextView wordView = (TextView) flipper.findViewById(R.id.word);
        if (!wordView.isShown()) {
            flipper.showPrevious();

        }
        flipper.requestFocus();
        flipper.setFocusableInTouchMode(true);
        CharSequence text = wordView.getText();
        Log.i(">>>>>>", "query with " + text);
        if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
            word = vocabulary.previous(text.toString());
            Log.i(">>>>>>", "the next one is " + word.getEng());
            TextView translationView = (TextView) flipper.findViewById(R.id.translation);
            wordView.setText(word.getEng());
            translationView.setText(word.getTranslation());
            return true;
        }
        if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
            word = vocabulary.next(text.toString());
            Log.i(">>>>>>", "the next one is " + word.getEng());
            TextView translationView = (TextView) flipper.findViewById(R.id.translation);
            wordView.setText(word.getEng());
            translationView.setText(word.getTranslation());
            return true;
        }


        return false;
    }
}
